package com.qk.management.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.ActivityDTO;
import com.qk.entity.Activity;
import com.qk.management.mapper.ActMapper;
import com.qk.management.service.ActService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ActServiceImpl implements ActService {

    @Autowired
    private ActMapper actMapper;


    @Override
    public PageResult<Activity> getActs(ActivityDTO actDTO) {
        PageHelper.startPage(actDTO.getPage(), actDTO.getPageSize());
        List<Activity> result = actMapper.selectByPage(actDTO);
        Page<Activity> page = (Page<Activity>) result;

        return PageResult.<Activity>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getResult())
                .build();
    }

    @Override
    public void addAct(Activity act) {
        act.setCreateTime(LocalDateTime.now());
        act.setUpdateTime(LocalDateTime.now());
        actMapper.insert(act);
    }

    @Override
    public Activity getActById(Integer id) {
        return actMapper.selectById(id);
    }

    @Override
    public void deleteAct(Integer id) {
        actMapper.deleteById(id);
    }

    @Override
    public void updateAct(Activity act) {
        act.setUpdateTime(LocalDateTime.now());
        actMapper.updateAct(act);
    }
}
